﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;

using Coding4Fun.Chair.Kinect;
using Coding4Fun.Kinect.Wpf;

using Microsoft.Kinect;

namespace Coding4Fun.Chair.KinectDiagnostic
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		Sensor _sensor = new Sensor();
	
		int _minDistance;

		public MainWindow()
		{
			InitializeComponent();
			slider1.Value = slider1.Minimum + slider1.Interval;

			InitializeSensor();
		}


		private void InitializeSensor()
		{
			_sensor.Start();
			_sensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(_sensor_AllFramesReady);
		}

		void _sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
		{
			_sensor_ColorFrameReady(e); 

		}

		void _sensor_ColorFrameReady(AllFramesReadyEventArgs e)
		{
			using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
			{
				ColorImage.Source = colorFrame.ToBitmapSource();
			}
			
		}

		void _sensor_DepthFrameReady(AllFramesReadyEventArgs e)
		{
			using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
			{
				DepthImage.Source = depthFrame.ToBitmapSource();

				var imageWidth = depthFrame.Width;
				var imageHeight = depthFrame.Height;

				var depthArray = depthFrame.ToDepthArray();


				DepthImageWithMinDistance.Source = depthArray.ToBitmapSource(imageWidth, imageHeight, _minDistance, Color.FromArgb(255, 255, 0, 0));

			}


		}

		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			_sensor.Stop(); 
		}

		private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
		{
			_minDistance = (int)e.NewValue;
		}
	}
}
